package web.composite;


import java.util.ArrayList;
import java.util.List;

public class Composite extends Component{
	
	public Composite(String name){
		this.name = name;
	}
	
	private List<Component> childComponents = null;
	
	//叶子实现实现数据的存储和行为
	public void someOperate(){
		if(childComponents != null){
			System.out.println("=="+this.name);
			for(Component c : childComponents){
				 c.someOperate();
			}
		}
	} 
	
	//进行叶子相关的管理
	public void addChild(Component child){
		if(childComponents == null){
			childComponents = new ArrayList<Component>();
		}
		childComponents.add(child);
	}
	
	public void removeChild(Component child){
		if(childComponents != null){
			childComponents.remove(child);
		}
	}
	
	public Component getChild(int child){
		if(childComponents != null){
			if(child >= 0 && childComponents.size()>child){
				return childComponents.get(child);
			} 
		} 
		return null;
	}

}
